import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path';
import * as fs from "node:fs";
import bodyParser from 'body-parser'

// https://vite.dev/config/
export default defineConfig({
  plugins: [
    vue(),

  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    },
  },
  server: {
    host: '0.0.0.0',
    port: 9090,
    proxy: {
      // 反向代理配置
      '/api': {
        // target: 'http://192.168.221.78:8081/api/',  // 后端接口地址
        target: 'http://10.5.39.10:8081/api/',
        changeOrigin: true,  // 允许修改请求头中的 origin 字段
        secure: false,  // 是否验证 SSL 证书，开发环境中可以设置为 false
        rewrite: (path) => path.replace(/^\/api/, ''), // 重写请求路径，去掉前缀 `/api`
      },
    },
    configureServer: (server) => {
      server.middlewares.use(bodyParser.json({ limit: '8mb' }))
      server.middlewares.use(bodyParser.urlencoded({ extended: true, limit: '8mb' }))
    }
    // https: {
    //   key: fs.readFileSync('private-key.pem'),
    //   cert: fs.readFileSync('certificate.pem'),
    // }
    // historyApiFallback: {
    //   // 这个配置可以确保开发环境中的路由都被正确处理
    //   rewrites: [
    //     { from: /^\/admin\/.*$/, to: '/admin.html' },
    //     { from: /^\/user\/.*$/, to: '/index.html' },
    //     { from: /./, to: '/index.html' }  // 如果其他路由不匹配，则回退到 index.html
    //   ]
    // },

  },
  build: {
    rollupOptions: {
      input: {
        client: resolve(__dirname, 'index.html'),
        admin: resolve(__dirname, 'admin.html'),
      },
    },
  },
})
